import React, { useEffect } from 'react'
import { Button, Card, Checkbox, Form, Input, message } from 'antd';
import style from '../index.module.css';
import { addHospitalRequest, getHosSetFromIdRequest ,updateHosSetFromIdRequest} from '@api/hospital/hospitalSet'
import type { addHospitalParamsType } from '@api/hospital/model/hospitalSetType'
import { useNavigate, useParams } from 'react-router-dom';
export default function AddHosSet() {
    const navigate = useNavigate()
    const { id } = useParams()
    const [form] = Form.useForm()
    useEffect(() => {
        if (id) {
            const getHosSetFromId = async () => {
                const re = await getHosSetFromIdRequest(id)
                form.setFieldsValue(re)
            }
            getHosSetFromId()
        }
    })
    const addHospitalFinishHandle = async (fieldValue: addHospitalParamsType) => {
        if(id){
            await updateHosSetFromIdRequest({...fieldValue,id})
            message.success('修改医院成功')
        }else{
            await addHospitalRequest(fieldValue)
            message.success('添加医院成功')
        }
         navigate('/syt/hospital/hospitalSet')
    }
    const goBackHandle = () => {
        navigate(-1)
    }
    return (
        <Card>
            <Form
                name="basic"
                labelCol={{
                    span: 3,
                }}
                wrapperCol={{
                    span: 16,
                }}
                onFinish={addHospitalFinishHandle}
                form={form}
            >
                <Form.Item
                    label="医院名称"
                    name="hosname"
                    rules={[{ required: true, message: '请输入医院名称' ,
                    transform(value){
                        return value.trim()
                    }
                }]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    label="医院编号"
                    name="hoscode"
                    rules={[{ required: true, message: '请输入医院编号' }]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    label="api基础路径"
                    name="apiUrl"
                    rules={[{ required: true, message: '请输入api基础路径' }]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    label="联系人姓名"
                    name="contactsName"
                    rules={[{ required: true, message: '请输入联系人姓名' }]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    label="联系人手机"
                    name="contactsPhone"
                    rules={[{ required: true, message: '请输入联系人手机' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    wrapperCol={{
                        offset: 3,
                        span: 16,
                    }}
                >
                    <Button type="primary" htmlType="submit" className={style.mr}>
                        提交
                    </Button>
                    <Button type="default" htmlType="submit" onClick={goBackHandle}>
                        返回
                    </Button>
                </Form.Item>
            </Form>
        </Card>

    );
}


